<template>
  <div class="homepage-hero-module">
    <div class="video-container">
      <div :style="fixStyle" class="filter">
        <div style="width: 400px; margin: 100px auto">
          <div
            style="
              font-size: 30px;
              text-align: center;
              padding: 30px 0;
              color: #333;
            "
          >
            欢迎登录
          </div>
          <el-form ref="form" :model="form" size="normal" :rules="rules">
            <el-form-item prop="username">
              <el-input
                prefix-icon="el-icon-user-solid"
                v-model="form.username"
                placeholder="请输入账号"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                prefix-icon="el-icon-lock"
                v-model="form.password"
                show-password
                placeholder="请输入密码"
              ></el-input>
            </el-form-item>
            <el-form-item >
              <!-- flex 布局  用于并排显示 -->
              <div style="display: flex">
                <el-input
                  prefix-icon="el-icon-key"
                  v-model="form.validCode"
                  style="width: 50%"
                  placeholder="请输入验证码"
                ></el-input>
                <!-- 验证码模块 -->
                <ValidCode @input="createValidCode" />
              </div>
            </el-form-item>
            <el-form-item>
              <el-radio v-model="form.role" :label="1" style="color: 	#A9A9A9"
                >管理员</el-radio
              >
              <el-radio v-model="form.role" :label="2" style="color: 	#A9A9A9"
                >普通用户</el-radio
              >
            </el-form-item>
            <el-form-item>
              <el-button style="width: 100%" type="primary" @click="login"
                >登 录</el-button
              >
            </el-form-item>
            <el-form-item
              >
              <!--跳转至注册界面 -->
              <el-button type="text" @click.native="$router.push('/register')" 
                >前往注册 >>
              </el-button></el-form-item
            >
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 导入请求跳转
import request from "@/utils/request";

// 导入验证码模块
import ValidCode from "@/components/validCode.vue";

export default {
  name: "Login",
  components:{
    ValidCode,
  },
  // 数据返回
  data() {
    return {
      vedioCanPlay: false,
      fixStyle: '',
      form: {role: 1},
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        
      },
      validCode: '',
       // 加背景图片
      // bg: {
      //   backgroundImage: "url(" + require("@/assets/bg.jpg") + ")",
      //   backgroundRepeat: "no-repeat",
      //   backgroundSize: "100% 100%"
      // }
    }
  },
   mounted() {
    sessionStorage.removeItem("user");//移除sessionStorage中的缓存信息

    window.onresize = () => {
      const windowWidth = document.body.clientWidth
      const windowHeight = document.body.clientHeight
      const windowAspectRatio = windowHeight / windowWidth
      let videoWidth
      let videoHeight
      if (windowAspectRatio < 0.5625) {
        videoWidth = windowWidth
        videoHeight = videoWidth * 0.5625
        this.fixStyle = {
          height: windowWidth * 0.5625 + 'px',
          width: windowWidth + 'px',
          'margin-bottom': (windowHeight - videoHeight) / 2 + 'px',
          'margin-left': 'initial'
        }
      } else {
        videoHeight = windowHeight
        videoWidth = videoHeight / 0.5625
        this.fixStyle = {
          height: windowHeight + 'px',
          width: windowHeight / 0.5625 + 'px',
          'margin-left': (windowWidth - videoWidth) / 2 + 'px',
          'margin-bottom': 'initial'
        }
      }
    }
    window.onresize()
  },
   methods: {
    canplay() {
      this.vedioCanPlay = true
    },
    // 接收验证码组件提交的 4位验证码
    createValidCode(data) {
      this.validCode = data
    },
    login() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (!this.form.validCode) {//非空判断
            this.$message.error("请填写验证码")
            return
          }
          if(this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()) {//验证码比较
            this.$message.error("验证码错误")
            return
          }
          request.post("/user/login", this.form).then(res => {//登录请求路径
            if ( res.data.code === '200') {
              this.$message({
                type: "success",
                message: "登录成功"
              })
              //存储在application 的session storage中
              sessionStorage.setItem("user", JSON.stringify( res.data.data))  // 缓存用户信息
              this.$router.push("/")  //登录成功之后进行页面的跳转，跳转到主页
            } else {
              this.$message({// 否则提示错误信息，不会跳转页面
                type: "error",
                message: res.data.msg
              })
            }
          })
        }
      })
    }
  }
  
}
</script>


<style scoped>
.homepage-hero-module,
.video-container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}

.video-container .poster img {
  z-index: 0;
  position: absolute;
}

.video-container .filter {
  z-index: 1;
  position: absolute;
  /*background: rgba(0, 0, 0, 0.4);*/
  width: 100%;
}

.fillWidth {
  width: 100%;
}
</style>